package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import util.db.DBConn;
import dao.TeacherMagDao;
import domain.Teacher;

/**
 * 
 * May 9, 2009
 *
 * @author Fabian
 *
 */
public class TeacherMagImpl implements TeacherMagDao {

	/* (non-Javadoc)
	 * @see dao.TeacherMagDao#modifyTeacher(vo.Teacher)
	 */
	
	public boolean modifyTeacher(Teacher t) {
		boolean result = false;
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "update t_teacher set name=?, sex=?, nickname=?, password=?, major_id=? where id = ?";
		try {
			conn = DBConn.getConn();
			pstmt = DBConn.getPstmt(conn, sql);
			pstmt.setString(1, t.getName());
			pstmt.setString(2, t.getSex());
			pstmt.setString(3, t.getNickname());
			pstmt.setString(4, t.getPassword());
			pstmt.setInt(5, t.getMajorId());
			pstmt.setInt(6, t.getId());
			int rsColum = pstmt.executeUpdate();		
			if (rsColum > 0) {
				result = true;
				return result;
			}
		} catch(SQLException e) {
			e.printStackTrace();
			return result;
		} finally {
			DBConn.close(conn);
			DBConn.close(pstmt);
			DBConn.close(rs);
		}
		
		return result;
	}

	/* (non-Javadoc)
	 * @see dao.TeacherMagDao#addTeacher(vo.Teacher)
	 */
	
	public boolean addTeacher(Teacher t) {
		boolean result = false;
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "insert into t_teacher values (null, ?, ?, ?, ?, ?)";
		try {
			conn = DBConn.getConn();
			pstmt = DBConn.getPstmt(conn, sql);
			pstmt.setString(1, t.getName());
			pstmt.setString(2, t.getSex());
			pstmt.setString(3, t.getNickname());
			pstmt.setString(4, t.getPassword());
			pstmt.setInt(5, t.getMajorId());
			int rsColum = pstmt.executeUpdate();		
			if (rsColum > 0) {
				result = true;
				return result;
			}
		} catch(SQLException e) {
			e.printStackTrace();
			return result;
		} finally {
			DBConn.close(conn);
			DBConn.close(pstmt);
			DBConn.close(rs);
		}
		
		return result;
	}

	/* (non-Javadoc)
	 * @see dao.TeacherMagDao#getPageTeacher(int, int)
	 */
	
	public List<Teacher> getPageTeacher(int start, int limit) {
		
		List<Teacher> list = new ArrayList<Teacher>();
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "select th.*, m.name as major_name from t_teacher th, t_major m where th.major_id = m.id order by th.id limit ?, ?";
		try {
			conn = DBConn.getConn();
			pstmt = DBConn.getPstmt(conn, sql);
			pstmt.setInt(1, start);
			pstmt.setInt(2, limit);
			rs = pstmt.executeQuery();			
			while (rs.next()) {
				Teacher th = new Teacher();
				th.setId(rs.getInt("id"));
				th.setName(rs.getString("name"));
				th.setSex(rs.getString("sex"));
				th.setNickname(rs.getString("nickname"));
				th.setPassword(rs.getString("password"));
				th.setMajorId(rs.getInt("major_id"));
				th.setMajor(rs.getString("major_name"));
				list.add(th);
			}
			return list;
		} catch(SQLException e) {
			e.printStackTrace();
			return null;
		} finally {
			DBConn.close(conn);
			DBConn.close(pstmt);
			DBConn.close(rs);
		}
	}

	/* (non-Javadoc)
	 * @see dao.TeacherMagDao#getTeacher(int)
	 */
	
	public Teacher getTeacher(int id) {
		Teacher th = null;
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "select t.*, m.name major_name from t_teacher t, t_major m where t.major_id = m.id and t.id = ?";
		try {
			conn = DBConn.getConn();
			pstmt = DBConn.getPstmt(conn, sql);
			pstmt.setInt(1, id);
			rs = pstmt.executeQuery();			
			if (rs.next()) {
				th = new Teacher();
				th.setId(rs.getInt("id"));
				th.setName(rs.getString("name"));
				th.setSex(rs.getString("sex"));
				th.setNickname(rs.getString("nickname"));
				th.setPassword(rs.getString("password"));
				th.setMajorId(rs.getInt("major_id"));
				th.setMajor(rs.getString("major_name"));
				return th;
			}
		} catch(SQLException e) {
			e.printStackTrace();
			return th;
		} finally {
			DBConn.close(conn);
			DBConn.close(pstmt);
			DBConn.close(rs);
		}
		
		return th;
	}

	/* (non-Javadoc)
	 * @see dao.TeacherMagDao#gettotalCount()
	 */
	
	public int getTotalCount() {
		int totalCount = 0;
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "select count(*) from t_teacher";
		try {
			conn = DBConn.getConn();
			pstmt = DBConn.getPstmt(conn, sql);
			rs = pstmt.executeQuery();			
			if (rs.next()) {
				totalCount = rs.getInt(1);
				return totalCount;
			}
		} catch(SQLException e) {
			e.printStackTrace();
			return totalCount;
		} finally {
			DBConn.close(conn);
			DBConn.close(pstmt);
			DBConn.close(rs);
		}
		
		return totalCount;
	}

	/* (non-Javadoc)
	 * @see dao.TeacherMagDao#getTeacher(java.lang.String, java.lang.String)
	 */
	
	public Teacher getTeacher(String nkname, String pwd) {
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "select * from t_teacher where nickname = ? and password = ?";
		try {
			conn = DBConn.getConn();
			pstmt = DBConn.getPstmt(conn, sql);
			pstmt.setString(1, nkname);
			pstmt.setString(2, pwd);
			rs = pstmt.executeQuery();			
			if (rs.next()) {
				Teacher th = new Teacher();
				th.setId(rs.getInt("id"));
				th.setName(rs.getString("name"));
				th.setSex(rs.getString("sex"));
				th.setNickname(rs.getString("nickname"));
				th.setPassword(rs.getString("password"));
				th.setMajorId(rs.getInt("major_id"));
				return th;
			}
		} catch(SQLException e) {
			e.printStackTrace();
			return null;
		} finally {
			DBConn.close(conn);
			DBConn.close(pstmt);
			DBConn.close(rs);
		}
		
		return null;
	}

	/* (non-Javadoc)
	 * @see dao.TeacherMagDao#delTeachers(java.lang.String)
	 */
	
	public boolean delTeachers(String thIdsStr) {
		boolean result = false;
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "delete from t_teacher where id in ( " + thIdsStr + " )";
		try {
			conn = DBConn.getConn();
			pstmt = DBConn.getPstmt(conn, sql);
			int rsColum = pstmt.executeUpdate();		
			if (rsColum > 0) {
				result = true;
				return result;
			}
		} catch(SQLException e) {
			e.printStackTrace();
			return result;
		} finally {
			DBConn.close(conn);
			DBConn.close(pstmt);
			DBConn.close(rs);
		}
		
		return result;
	}
	
}
